package com.starblink.search.result.ui.fragment.goods

import android.content.res.ColorStateList
import android.graphics.Rect
import android.graphics.Typeface
import android.graphics.drawable.RotateDrawable
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.core.view.updateLayoutParams
import androidx.drawerlayout.widget.DrawerLayout
import androidx.recyclerview.widget.LinearLayoutManager
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.ext.bindProductFeeds
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.color
import com.starblink.android.basic.ext.getRealNavBarHeight
import com.starblink.android.basic.ext.gone
import com.starblink.android.basic.ext.string
import com.starblink.android.basic.ext.visible
import com.starblink.android.basic.ext.visibleOrGone
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.android.basic.sensorsdata.extension.trackData
import com.starblink.android.basic.sensorsdata.room.entity.SkAntEntity
import com.starblink.android.basic.sensorsdata.spm.GTrackerAssistUtils
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmPageDef
import com.starblink.android.basic.sensorsdata.spm.event.TrackEvent
import com.starblink.android.basic.sensorsdata.spm.impl.SpmTrackHandler
import com.starblink.android.basic.util.post.CommonMutateManage
import com.starblink.android.common.databinding.CellSearchBrandBinding
import com.starblink.android.common.view.brand.BrandResultCell
import com.starblink.basic.apollo.ext.ApolloValSetExt
import com.starblink.basic.ext.inRect
import com.starblink.basic.ext.screenWidth
import com.starblink.basic.style.view.recyclerview.SimpleGapItemDecor
import com.starblink.library.widget.product.ProductCardScene
import com.starblink.library.widget.product.RecommendProductCardCell
import com.starblink.library.widget.product.SimpleProductCardCell
import com.starblink.library.widget.product.util.TrackUtil
import com.starblink.rocketreserver.fragment.BrandVOF
import com.starblink.search.R
import com.starblink.search.config.SearchConfig
import com.starblink.search.databinding.CellDrawerPriceBinding
import com.starblink.search.databinding.CellDrawerPropertyBinding
import com.starblink.search.databinding.CellDrawerTitleBinding
import com.starblink.search.databinding.CellSearchFrontPropertyBinding
import com.starblink.search.databinding.DrawerGoodsFilterBinding
import com.starblink.search.databinding.FragmentGoodsResultBinding
import com.starblink.search.databinding.LayoutErrWordBinding
import com.starblink.search.result.repository.model.IFrontProperty
import com.starblink.search.result.repository.model.SelectDiscountProperty
import com.starblink.search.result.ui.cell.DrawerBrandCell
import com.starblink.search.result.ui.cell.DrawerInputCell
import com.starblink.search.result.ui.cell.DrawerPropertyCell
import com.starblink.search.result.ui.cell.DrawerTitleCell
import com.starblink.search.result.ui.cell.GoodsEmptyCell
import com.starblink.search.result.ui.cell.GoodsTitleCell
import com.starblink.search.result.ui.cell.SearchFrontBrandCell
import com.starblink.search.result.ui.cell.SearchFrontDiscountCell
import com.starblink.search.result.ui.cell.SearchFrontPropertyCell
import com.starblink.search.result.ui.fragment.BaseSearchFragment
import com.starblink.search.result.ui.widget.FilterPropertyDecoration
import com.starblink.search.result.ui.widget.GoodsSortPop
import razerdp.basepopup.BasePopupWindow

/**
 * 商品搜索结果
 */
class GoodsResultFragment :
    BaseSearchFragment<FragmentGoodsResultBinding, GoodsResultVM>(),
    DrawerLayout.DrawerListener {

    private lateinit var errWordBinding: LayoutErrWordBinding

    private val adapter by lazyOnNone {
        MutableAdapter(viewModel.contentList).apply {
            addVhDelegate {
                SimpleProductCardCell(
                    viewBinding.refresher,
                    viewBinding.recyclerContent,
                    it,
                    if (viewModel.scene == SearchConfig.SCENE_CATEGORY) ProductCardScene.CATEGORY_GOODS else ProductCardScene.SEARCH,
                    pageTrackTag = this@GoodsResultFragment.hashCode().toString(),
                    searchWord = viewModel.queryWord,
                    searchWordSource = viewModel.searchWordSource
                )
            }
            addVhDelegate {
                GoodsEmptyCell(it)
            }
            addVhDelegate {
                GoodsTitleCell(it)
            }
            addVhDelegate {
                RecommendProductCardCell(
                    refreshLayout = viewBinding.refresher,
                    recyclerView = viewBinding.recyclerContent,
                    it, ProductCardScene.SEARCH_RECOMMEND,
                    pageTrackTag = this@GoodsResultFragment.hashCode().toString()
                )
            }
        }
    }

    private val propertyAdapter by lazyOnNone {
        MutableAdapter(viewModel.frontPropertyContentList).apply {
            addVhDelegate {
                SearchFrontPropertyCell(
                    CellSearchFrontPropertyBinding.inflate(
                        layoutInflater,
                        it,
                        false
                    )
                ) { model, position ->
                    updateFrontPropertyModel(model, position)
                }
            }
            addVhDelegate {
                SearchFrontBrandCell(
                    CellSearchFrontPropertyBinding.inflate(
                        layoutInflater,
                        it,
                        false
                    )
                ) { model, position ->
                    updateFrontPropertyModel(model, position)
                }
            }
            addVhDelegate {
                SearchFrontDiscountCell(
                    CellSearchFrontPropertyBinding.inflate(
                        layoutInflater,
                        it,
                        false
                    )
                ) { ->
                    updateDiscountModel()
                }
            }
        }
    }

    private val drawerAdapter by lazyOnNone {
        MutableAdapter(viewModel.drawerContentList).apply {
            addVhDelegate {
                DrawerTitleCell(
                    CellDrawerTitleBinding.inflate(layoutInflater, it, false)
                )
            }
            addVhDelegate {
                DrawerInputCell(CellDrawerPriceBinding.inflate(layoutInflater, it, false))
            }
            addVhDelegate {
                DrawerBrandCell(CellDrawerPropertyBinding.inflate(layoutInflater, it, false))
            }
            addVhDelegate {
                DrawerPropertyCell(CellDrawerPropertyBinding.inflate(layoutInflater, it, false))
            }
        }
    }

    private val filterBinding: DrawerGoodsFilterBinding by lazyOnNone {
        DrawerGoodsFilterBinding.inflate(layoutInflater)
    }

    /**排序弹窗*/
    private val sortPop by lazyOnNone {
        GoodsSortPop(requireContext()) { sort, text ->
            // 选择排序选项后，重新加载数据
            viewModel.sortType = sort
            viewBinding.btnSort.text = text
            viewModel.loadFilterRequest()
        }
    }

    private val rectFrontProperty = Rect()
    private val locationArrayTag = intArrayOf(0, 0)

    private var trackable = true

    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?,
    ): FragmentGoodsResultBinding =
        FragmentGoodsResultBinding.inflate(LayoutInflater.from(requireContext())).apply {
            errWordBinding = LayoutErrWordBinding.bind(this.root)
        }

    override fun emptyMessage(): String = viewModel.emptyString


    override fun initData() {
        arguments?.run {
            getString(KEY_WORD)?.let {
                viewModel.queryWord = it
            }
            getString(CATEGORY_NAME)?.let {
                viewModel.categoryName = it
            }
            getInt(KEY_SEARCH_WORD_SOURCE)?.let {
                viewModel.searchWordSource = it
            }
            getString("goodsRuleId")?.let {
                viewModel.goodsRuleId = it
            }
            viewModel.scene = getInt(KEY_SCENE, SearchConfig.SCENE_ALL)
            trackable = getBoolean(KEY_TRACK, true)
        }
    }

    override fun initView(view: View) {
        viewBinding.recyclerProperty.adapter = propertyAdapter
        viewBinding.recyclerProperty.layoutManager =
            LinearLayoutManager(this.requireContext(), LinearLayoutManager.HORIZONTAL, false)
        viewBinding.recyclerProperty.addItemDecoration(
            SimpleGapItemDecor(
                withEdge = true,
                horGap = 8.dp2px(),
                edgeGap = 16.dp2px()
            )
        )
        viewBinding.recyclerProperty.addItemDecoration(
            FilterPropertyDecoration(color(com.starblink.basic.style.R.color.sk_gray50))
        )
        viewBinding.recyclerContent.bindProductFeeds(adapter)
        //viewBinding.recyclerContent.bindFeedsNoviceHelper(requireActivity())
        viewBinding.refresher.setOnLoadMoreListener {
            if (viewModel.resultList.isNotEmpty()) {
                viewModel.loadMore()
            } else if (viewModel.recommendList.isNotEmpty()) {
                viewModel.loadMoreForU()
            }

        }
        viewBinding.refresher.setEnableAutoLoadMore(true)
        initSort()
        initDrawer()
    }


    override fun onLazyInitData() {
        super.onLazyInitData()
        viewModel.loadInitData()
    }

    override fun initLoad() {

    }

    var brandResultCell: BrandResultCell? = null
    override fun startObserve() {
        viewModel.brandResult.observe(this) {
            viewModel.brandResult.value?.let { it1 ->
                brandResultCell = BrandResultCell(
                    CellSearchBrandBinding.inflate(
                        layoutInflater,
                        viewBinding.content,
                        false
                    ),
                    pageDef = SpmPageDef.SearchGoodsPage,
                    searchTerm = viewModel.queryWord,
                    searchType = if (viewModel.searchWordSource == SearchConfig.SearchWordSourceConfig.O_TYPE_HOT) "trendingWord" else if (viewModel.searchWordSource == SearchConfig.SearchWordSourceConfig.O_TYPE_SHADING) "shadingWord" else "normal",
                ) { merchant, position ->
                    merchant.brandId?.let { it1 ->
                        CommonMutateManage.subscribeOrNotBrand(
                            requireContext(), merchant.hasSub,
                            it1, brand = merchant,
                            resultCallback = {
                                ApolloValSetExt.setApolloVal<BrandVOF>(
                                    merchant, "hasSub",
                                    !merchant.hasSub
                                )
                                ApolloValSetExt.setApolloVal<BrandVOF>(
                                    merchant, "subNums",
                                    if (merchant.hasSub) (merchant.subNums!! + 1) else (merchant.subNums!! - 1)
                                )
                                brandResultCell?.onBind(merchant, 0)
                            })
                    }
                }
                val itemView = brandResultCell!!.itemView
                viewBinding.brandCell.addView(itemView)
                viewBinding.brandCell.visibleOrGone(true)

                brandResultCell!!.onBind(it1, 0)
            }
        }
        viewModel.resultLD.observe(this) {
            if (viewModel.pageNo == 1) {
                viewBinding.recyclerContent.scrollToPosition(0)
            }
            viewBinding.refresher.finishLoadMore()
            if (it.isNullOrEmpty()) {
//                viewBinding.refresher.finishLoadMoreWithNoMoreData()
            } else {
                val indexTag = adapter.itemCount
                viewModel.contentList.addAll(it)
                if (indexTag == 0) {
                    adapter.refreshAll()
                } else {
                    adapter.notifyItemRangeInserted(indexTag, it.size)
                }
            }

            viewBinding.vFilterBg.visibleOrGone(viewModel.filterGroupVisible) {
                updateFilterBtn(false)
            }
            viewBinding.recyclerProperty.visibleOrGone(viewModel.frontPropertyVisible) {
                propertyAdapter.refreshAll()
            }
            viewBinding.btnFilter.visibleOrGone(viewModel.filterVisible) {

            }
        }
        viewModel.drawerDataLD.observe(this) {
            drawerAdapter.refreshAll()
        }
        viewModel.recommendLD.observe(this) {
            if (viewModel.recPageNo == 1) {
                viewBinding.recyclerContent.scrollToPosition(0)
                viewModel.setEmptyText()
                viewModel.contentList.add(viewModel.emptyModel)
                it?.takeIf { it.isNotEmpty() }?.run {
                    viewModel.contentList.add(viewModel.recTitleModel)
                }
            }
            viewBinding.refresher.finishLoadMore()

            if (it.isNullOrEmpty()) {
                viewBinding.refresher.finishLoadMoreWithNoMoreData()
            } else {
                val indexTag = adapter.itemCount
                viewModel.contentList.addAll(it)
                if (indexTag < 3) {
                    adapter.refreshAll()
                } else {
                    adapter.notifyItemRangeInserted(indexTag, it.size)
                }
            }
        }

        viewModel.errWordLiveData.observe(this) {
//            errWordBinding.groupErrorWord.visible()
//            errWordBinding.tvError1.buildSpannableString {
//                addText(string(R.string.search_err_tip1_span1))
//                addText(it) {
//                    FontUtil.getTypeface(FontUtil.DM_SANS_BOLD)?.let {
//                        setTypeface(it)
//                    }
//                }
//                addText(string(R.string.search_err_tip1_span2))
//            }
//            errWordBinding.btnError.text = viewModel.queryWord
//            errWordBinding.btnError.setOnClickListener {
//                errWordBinding.groupErrorWord.gone()
//                viewModel.loadErrWordData()
//            }
        }

        viewModel.filterSaleVisibleLiveData.observe(this) {
            viewBinding.clFilterSale.visibleOrGone(it)
            viewBinding.lineFilter.visibleOrGone(it)
        }
    }

    /**初始化排序区域相关控件*/
    private fun initSort() {
        viewBinding.btnSort.icon = RotateDrawable().apply {
            this.drawable =
                ContextCompat.getDrawable(
                    requireContext(),
                    com.starblink.basic.style.R.drawable.ic_svg_arrow_down_16_16
                )
        }

        viewBinding.btnSort.setOnClickListener {
            if (!viewModel.filterSale) {
                sortPop.linkTo(viewBinding.vFilterBg)
                sortPop.showPopupWindow(viewBinding.vFilterBg)
            }
        }
        sortPop.setOnPopupWindowShowListener {
            (viewBinding.btnSort.icon as RotateDrawable?)?.let {
                it.level = 10000 / 2
            }
        }
        sortPop.onDismissListener = object : BasePopupWindow.OnDismissListener() {
            override fun onDismiss() {
                (viewBinding.btnSort.icon as RotateDrawable?)?.let {
                    it.level = 0
                }
            }
        }

        if (arguments?.getBoolean(IS_SALE_ZONE) == true) {
            viewModel.filterSale = true
            changeSaleView()
        }

        // 埋点：搜索结果页-sale按钮
        viewBinding.clFilterSale.trackData(
            page = TrackUtil.transformPageValue(if (viewModel.scene == SearchConfig.SCENE_CATEGORY) ProductCardScene.CATEGORY_GOODS else ProductCardScene.SEARCH),
            element = SpmElementDef.SEARCH_RESULT_SALE_BUTTON,
            params = mapOf(if (viewModel.scene == SearchConfig.SCENE_CATEGORY) "frontName" to viewModel.categoryName else "searchTerm" to viewModel.queryWord),
        )
        viewBinding.clFilterSale.click {
            // 选中/取消选中Sale，更新数据
            viewModel.filterSale = !viewModel.filterSale

            //显示Discount按钮
            viewModel.sortType = SearchConfig.SORT_DEFAULT
            viewBinding.btnSort.text = string(com.starblink.basic.style.R.string.recommended)
            changeSaleView()

            viewModel.loadFilterRequest()
        }
//        viewBinding.btnSaleSort.click {
//            // 0-默认排序，1-从大到小，2-从小到大
//            viewModel.saleSort = (viewModel.saleSort++) % 3
//            viewBinding.btnSaleSort.text = "Discount${viewModel.saleSort}"
//            viewModel.loadFilterRequest()
//        }

        viewBinding.frontFilterView.run {
            onSelect = {
                propertyAdapter.refreshAll()
                viewModel.buildReqBrandAndProperty()
                viewModel.loadFilterRequest()
                refreshDrawer()
                updateFilterBtn()
            }

            onDismiss = {
                viewModel.brandWrap?.let {
                    it.select = false
                    propertyAdapter.refreshAll()
                }
                viewModel.propertyList.forEach {
                    it.select = false
                    propertyAdapter.refreshAll()
                }
            }
        }
    }

    private fun changeSaleView() {
        viewBinding.btnFilterSale.setImageResource(if (viewModel.filterSale) R.drawable.ic_sale_choosed else R.drawable.ic_sale_choose)
        viewBinding.ivSale.visibleOrGone(viewModel.filterSale)
        viewBinding.lottieSale.visibleOrGone(!viewModel.filterSale)

        viewModel.addDiscountItem(viewModel.filterSale)
        propertyAdapter.refreshAll()

        viewBinding.btnSort.icon = if (viewModel.filterSale) null else RotateDrawable().apply {
            drawable = context?.let {
                ContextCompat.getDrawable(
                    it,
                    com.starblink.basic.style.R.drawable.ic_svg_arrow_down_fill
                )
            }
        }
    }

    private fun initDrawer() {
        viewBinding.btnFilter.setOnClickListener {
            openDrawer()
        }
        viewBinding.btnFilter.trackData(
            page = SpmPageDef.SearchGoodsPage,
            element = SpmElementDef.Element_30096,
            params = mapOf(
                "searchType" to if (viewModel.searchWordSource == SearchConfig.SearchWordSourceConfig.O_TYPE_HOT) "trendingWord" else if (viewModel.searchWordSource == SearchConfig.SearchWordSourceConfig.O_TYPE_SHADING) "shadingWord" else "normal",
                "searchTerm" to viewModel.queryWord
            ),
//            businessSuffixID = businessSuffixId(viewModel.queryWord)
        )

        // 为了方便数据处理，这里骚操作直接放在Activity上
        viewBinding.root.post {
            (activity?.window?.decorView as FrameLayout?)?.addView(
                filterBinding.root,
                ViewGroup.MarginLayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                ).apply {
                    bottomMargin = this@GoodsResultFragment.requireActivity().getRealNavBarHeight()
                })
        }
        filterBinding.root.gone()
        filterBinding.drawer.addDrawerListener(this)
        filterBinding.filterLayout.updateLayoutParams<DrawerLayout.LayoutParams> {
            width = (screenWidth * (327.0 / 375.0)).toInt()
        }
        filterBinding.vDrawerClose.updateLayoutParams<DrawerLayout.LayoutParams> {
            width = (screenWidth * (48.0 / 375.0)).toInt()
        }
        filterBinding.vDrawerClose.setOnClickListener {
            closeDrawer()
        }
        filterBinding.recycler.layoutManager = LinearLayoutManager(requireContext())
        filterBinding.recycler.adapter = drawerAdapter
        filterBinding.btnDone.setOnClickListener {
            viewModel.drawerDoneClickTag = true
            startLoadFilterRequest()
            closeDrawer()
            propertyAdapter.refreshAll()
        }
        filterBinding.btnClear.setOnClickListener {
            viewModel.drawerClearClickTag = true
            viewModel.clearDrawerData()
            refreshDrawer()
        }

    }

    private fun startLoadFilterRequest() {
        viewModel.loadFromDrawerData()
        viewModel.buildReqBrandAndProperty()
        viewModel.loadFilterRequest()
        updateFilterBtn()
    }

    private fun updateFilterBtn(
        needUpdateRefresher: Boolean = true,
    ) {
        val hasFilterSelect = viewModel.hasFilterSelect
        val color =
            color(if (hasFilterSelect) com.starblink.basic.style.R.color.gu_text_primary else com.starblink.basic.style.R.color.gu_text_secondary)
        viewBinding.btnFilter.iconTint = ColorStateList.valueOf(color)
        viewBinding.btnFilter.setTextColor(color)
        viewBinding.btnFilter.typeface =
            Typeface.defaultFromStyle(if (hasFilterSelect) Typeface.BOLD else Typeface.NORMAL)

        if (needUpdateRefresher) {
            viewBinding.refresher.setNoMoreData(false)
        }
    }

    private fun refreshDrawer() {
        viewModel.refreshDrawerData()
        drawerAdapter.refreshAll()
    }

    private fun openDrawer() {
        filterBinding.root.visible()
        filterBinding.drawer.openDrawer(filterBinding.filterLayout)
        SpmTrackHandler.addSingleTrackData(
            SkAntEntity(
                event = TrackEvent.pageExposure.value,
                spmCnt = GTrackerAssistUtils.fetchPageValue(SpmPageDef.SearchGoodFilterDrawer),
            )
        )
    }

    private fun closeDrawer() {
        filterBinding.drawer.closeDrawer(filterBinding.filterLayout)
    }

    override fun onDrawerSlide(drawerView: View, slideOffset: Float) {

    }

    override fun onDrawerOpened(drawerView: View) {
        filterBinding.root.visible()
        viewModel.drawerClearClickTag = false
        viewModel.drawerDoneClickTag = false
    }

    override fun onDrawerClosed(drawerView: View) {
        filterBinding.root.gone()
        if (viewModel.drawerClearClickTag) {
            propertyAdapter.refreshAll()
            startLoadFilterRequest()
        } else if (viewModel.drawerDoneClickTag) {
            refreshDrawer()
        } else {
            refreshDrawer()
        }
    }

    override fun onDrawerStateChanged(newState: Int) {
//        if (newState == DrawerLayout.STATE_IDLE) {
//        }
    }


    fun onParentViewTouch(event: MotionEvent) {
        if (!event.inRect(rectFrontProperty.apply {
                viewBinding.recyclerProperty.getLocationInWindow(locationArrayTag)
                left = viewBinding.recyclerProperty.left
                top = locationArrayTag[1]
                right = viewBinding.recyclerProperty.right
                bottom = locationArrayTag[1] + viewBinding.recyclerProperty.height
            }) && !event.inRect(rectFrontProperty.apply {
                viewBinding.frontFilterView.getLocationInWindow(locationArrayTag)
                top = locationArrayTag[1]
                bottom = locationArrayTag[1] + viewBinding.frontFilterView.height
            })) {
            viewBinding.frontFilterView.dismiss()
        }

    }

    private fun updateFrontPropertyModel(model: IFrontProperty, position: Int) {
        viewModel.frontPropertyContentList.forEachIndexed { index, selectablePropertyWrap ->
            if (selectablePropertyWrap is IFrontProperty) {
                (selectablePropertyWrap as IFrontProperty?)?.run {
                    setSelected(index == position)
                }
            }
        }
        if (model == viewBinding.frontFilterView.data && viewBinding.frontFilterView.isShowing()) {
            model.setSelected(!model.getSelected())
            viewBinding.frontFilterView.dismiss()
        } else {
            viewBinding.frontFilterView.setupData(model)
            viewBinding.frontFilterView.show()
        }
        viewBinding.recyclerProperty.adapter?.notifyDataSetChanged()
        viewBinding.recyclerProperty.scrollToPosition(position)
    }

    private fun updateDiscountModel() {
        // 选择Discount排序选项后，重新加载数据
        if (viewModel.sortType == SearchConfig.SORT_DISCOUNT_DOWN) {
            viewModel.sortType = SearchConfig.SORT_DISCOUNT_UP
        } else if (viewModel.sortType == SearchConfig.SORT_DISCOUNT_UP) {
            viewModel.sortType = SearchConfig.SORT_DEFAULT
        } else {
            viewModel.sortType = SearchConfig.SORT_DISCOUNT_DOWN
        }
        viewBinding.btnSort.text = string(com.starblink.basic.style.R.string.recommended)

        if (viewModel.frontPropertyContentList.size > 0 && viewModel.frontPropertyContentList.first() is SelectDiscountProperty) {
            var selectDiscountProperty: SelectDiscountProperty =
                viewModel.frontPropertyContentList.first() as SelectDiscountProperty
            selectDiscountProperty.sortType = viewModel.sortType
//            viewModel.frontPropertyContentList[0] = selectDiscountProperty
        }
        viewBinding.recyclerProperty.adapter?.notifyDataSetChanged()
        viewBinding.recyclerProperty.scrollToPosition(0)

        viewModel.loadFilterRequest()
    }

    override fun onDestroyView() {
        (filterBinding.root.parent as ViewGroup).removeView(filterBinding.root)
        super.onDestroyView()
    }

    override fun spmPageId(): Int = SpmPageDef.SearchGoodsPage
    override fun isSpmTrackWithLifecycle(): Boolean = true
    override fun spmPageParams(): Map<String, String> = mutableMapOf(
        "searchTerm" to viewModel.queryWord,
        "searchType" to if (viewModel.searchWordSource == SearchConfig.SearchWordSourceConfig.O_TYPE_HOT) "trendingWord" else if (viewModel.searchWordSource == SearchConfig.SearchWordSourceConfig.O_TYPE_SHADING) "shadingWord" else "normal"
    )

    override fun onResume() {
        super.onResume()
    }

    companion object {

        private const val TAG = "GoodsResultFragment"

        private const val KEY_WORD = "word"
        private const val CATEGORY_NAME = "categoryName"
        private const val KEY_SEARCH_WORD_SOURCE = "search_word_source"
        private const val KEY_SCENE = "scene"
        private const val KEY_TRACK = "page_track"
        private const val IS_SALE_ZONE = "isSaleZone"

        fun newInstance(
            keyword: String,
            categoryName: String? = null,
            searchWordSource: Int? = null,
            @SearchConfig.Scene scene: Int = SearchConfig.SCENE_ALL,
            pageTrack: Boolean = true,
            isSaleZone: Boolean = false,
            goodsRuleId: String? = null,
        ) =
            GoodsResultFragment().apply {
                arguments = bundleOf(
                    KEY_WORD to keyword,
                    CATEGORY_NAME to categoryName,
                    KEY_SEARCH_WORD_SOURCE to searchWordSource,
                    KEY_SCENE to scene,
                    IS_SALE_ZONE to isSaleZone,
                    KEY_TRACK to pageTrack,
                    "goodsRuleId" to goodsRuleId,
                )
            }
    }
}